﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;

using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class NewNatSelLink : Form
    {
        private Model model;
        private PhenotypeTable phen_t=null;
        private string phname, path;
        private decimal amount;
        private bool b = false,trade=false;
        private MyDeleg deleg1,deleg2;
        public NewNatSelLink(Model m)
        {
            InitializeComponent();
            model = m;
        }
        public NewNatSelLink(Model m,string n, string p, decimal d)
        {
            InitializeComponent();
            model = m;
            phname = n;
            path = p;
            amount = d;
            b = true;
        }
        public NewNatSelLink(Model m, string n, string p, decimal d, MyDeleg del1, MyDeleg del2)
        {
            InitializeComponent();
            model = m;
            phname = n;
            path = p;
            amount = d;
            deleg1 = del1;
            deleg2 = del2;
            b = true;
            trade = true;
        }
        private void create_Click(object sender, EventArgs e)
        {
            if ((phen_t_box.SelectedItem==null)&&(phen_t_box.Text == ""))
            {
                MessageBox.Show("Please, choose phenotype table.");
                return;
            }
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A natural selection link with this name already exists. Please, choose another name.");
                return;
            }
            if (trade)
            {
                NewReprodLink link = new NewReprodLink(model, path, "file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml", name_box.Text, amount, deleg2);
                if (link.ShowDialog(this) != DialogResult.OK)
                {
                    MessageBox.Show("Unable to create natural selection link without reproduction link.");
                    return;
                }
            }
            else
            {
                NewReprodLink link = new NewReprodLink(model, path, "file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml", name_box.Text, amount);
                if (link.ShowDialog(this) != DialogResult.OK)
                {
                    MessageBox.Show("Unable to create natural selection link without reproduction link.");
                    return;
                }
            }

            if (!b)
            {
                foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
                {
                    if ((string)phen_t_box.SelectedItem == t.name)
                    {
                        phen_t = t;
                        path = phen_t.path;
                        amount = phen_t.characteristics_amount;
                    }
                }
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("link");
            writer.WriteAttributeString("Type", "natural_selection");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("PathToPhenotypeTable", path);
            writer.WriteAttributeString("CharacteristicsAmount", amount.ToString());
            writer.WriteStartElement("characteristic_names");
            if (!b)
            {
                int[] values = new int[phen_t.characteristics_amount];
                int j = -1;
                foreach (CharacteristicDescription charac in phen_t.characteristic_table.Values)
                {
                    j++;
                    writer.WriteString(charac.name);
                    if (charac != phen_t.characteristic_table.Values.Last())
                        writer.WriteString(" ");
                    values[j] = charac.max_degree;
                }
                writer.WriteEndElement();
                writer.WriteStartElement("characteristic_value_diapasons");
                foreach (CharacteristicDescription charac in phen_t.characteristic_table.Values)
                {
                    writer.WriteString(charac.max_degree.ToString());
                    if (charac != phen_t.characteristic_table.Values.Last())
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("event");
                writer.WriteAttributeString("Id", "0");
                writer.WriteAttributeString("Name", "Having Progency");
                writer.WriteStartElement("characteristic_sets");
                List<List<int>> comb =Functions.combine(values);
                foreach (List<int> l in comb)
                {
                    foreach (int i in l)
                    {
                        writer.WriteString(" " + i.ToString());
                    }
                    writer.WriteString(";");
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteStartElement("event");
                writer.WriteAttributeString("Id", "1");
                writer.WriteAttributeString("Name", "No Having Progency");
                writer.WriteStartElement("characteristic_sets");
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            else
            {
                for(int i=0;i<amount;i++)
                {
                    writer.WriteString("Char_"+i.ToString());
                    if (i != amount-1)
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("characteristic_value_diapasons");
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteString("0");
                    if (i != amount - 1)
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("event");
                writer.WriteAttributeString("Id", "0");
                writer.WriteAttributeString("Name", "Having Progency");
                writer.WriteStartElement("characteristic_sets");
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteString(" 0");
                }
                writer.WriteString(";");
                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteStartElement("event");
                writer.WriteAttributeString("Id", "1");
                writer.WriteAttributeString("Name", "No Having Progency");
                writer.WriteStartElement("characteristic_sets");
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("link");
            el.SetAttribute("Id", model.model_natural_selection_links.Count.ToString());
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("links")[0].Attributes[2]);
            doc.Increment(doc.DocumentElement.Attributes[2]);
            doc.GetElementsByTagName("natural_selection_links")[0].AppendChild(el);
            doc.Save(model.model_path);
            if (trade)
                deleg1("file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml");
            Functions.add_node_func(new NaturalSelectionLink("file_input\\" + model.model_name + "\\Links\\NaturalSelectionLinks\\" + name_box.Text + ".xml"), name_box.Text, "natsell");
            this.Close();
             
        }
        
        private void canc_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void NewNatSelLink_Load(object sender, EventArgs e)
        {
            if (b)
            {
                phen_t_box.Text = phname;
                phen_t_box.Enabled = false;
            }
            else
            {
                foreach (PhenotypeTable t in model.model_phenotype_tables.Values)
                {
                    phen_t_box.Items.Add(t.name);
                }
            }
        }
    }
}
